package org.listbeast.gui;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.ErrorDialog;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.swt.widgets.ProgressBar;
import org.listbeast.core.common.ApplicationException;
import org.listbeast.core.ListBeastApp;
import org.listbeast.core.observer.ListBeastGUIObserver;
import org.listbeast.core.observer.NotificationData;
import org.listbeast.core.observer.NotificationType;
import org.eclipse.wb.swt.SWTResourceManager;

public class ListBeastSWT implements ListBeastGUIObserver {

	protected Shell shlListBeast;
	private Text txtSelectSourceFolder;
	private Text sourceDirText;
	private FormToolkit formToolkit;
	private Text txtMaximumNumberOf;
	private Text txtSelectDestFolder;
	private Text destDirText;
	private Button btnCopySelectedSongs;
	private Combo maxYoutubeResults;
	private Button startButton;
	private ProgressBar progressBar;
	private Display display;
	private Text progressStatusText;
	private Button btnCreateMultiplePlaylists;

	/**
	 * Open the window.
	 * @wbp.parser.entryPoint
	 */
	public void open() {
		formToolkit = new FormToolkit(Display.getDefault());
		display = Display.getDefault();
		createContents();
		shlListBeast.open();
		shlListBeast.layout();
		while (!shlListBeast.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shlListBeast = new Shell(SWT.SHELL_TRIM & ~ SWT.RESIZE);
		shlListBeast.setSize(486, 450);
		shlListBeast.setText("List-Beast");
		shlListBeast.setLayout(new GridLayout(2, false));

		Menu menu = new Menu(shlListBeast, SWT.BAR);
		shlListBeast.setMenuBar(menu);

		MenuItem mntmHelp = new MenuItem(menu, SWT.CASCADE);
		mntmHelp.setText("Help");

		Menu menu_1 = new Menu(mntmHelp);
		mntmHelp.setMenu(menu_1);

		MenuItem mntmNewItem = new MenuItem(menu_1, SWT.NONE);
		mntmNewItem.setText("Tips");

		MenuItem mntmNewItem_1 = new MenuItem(menu_1, SWT.NONE);
		mntmNewItem_1.setText("About");

		txtSelectSourceFolder = new Text(shlListBeast, SWT.NONE);
		txtSelectSourceFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
		txtSelectSourceFolder.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
				false, false, 2, 1));
		txtSelectSourceFolder.setEditable(false);
		txtSelectSourceFolder
				.setText("Select source folder containing your music files");

		GridData data = new GridData();
		data.grabExcessHorizontalSpace = true;
		data.horizontalAlignment = GridData.FILL;
		data.verticalAlignment = GridData.BEGINNING;

		sourceDirText = new Text(shlListBeast, SWT.BORDER);
		GridData gd_sourceDirText = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_sourceDirText.widthHint = 200;
		sourceDirText.setLayoutData(gd_sourceDirText);
		Button sourceButton = new Button(shlListBeast, SWT.PUSH);
		sourceButton.setText("Browse...");
		sourceButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shlListBeast);
				String platform = SWT.getPlatform();
				dialog.setFilterPath(platform.equals("win32")
						|| platform.equals("wpf") ? "c:\\" : "/");
				String sourceDirectory = dialog.open();
				if (sourceDirectory != null) {
					sourceDirText.setText(sourceDirectory);
				}
			}
		});

		txtSelectDestFolder = new Text(shlListBeast, SWT.NONE);
		txtSelectDestFolder.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
		txtSelectDestFolder.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER,
				false, false, 2, 1));
		txtSelectDestFolder.setEditable(false);
		txtSelectDestFolder
				.setText("Select destination folder to store your playlist(s)");

		destDirText = new Text(shlListBeast, SWT.BORDER);
		GridData gd_setDirText = new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1);
		gd_setDirText.widthHint = 180;
		destDirText.setLayoutData(gd_setDirText);
		formToolkit.adapt(destDirText, true, true);

		Button destButton = new Button(shlListBeast, SWT.NONE);
		destButton.setText("Browse...");
		destButton.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				DirectoryDialog dialog = new DirectoryDialog(shlListBeast);
				String platform = SWT.getPlatform();
				dialog.setFilterPath(platform.equals("win32")
						|| platform.equals("wpf") ? "c:\\" : "/");
				String destinationDirectory = dialog.open();
				if (destinationDirectory != null) {
					destDirText.setText(destinationDirectory);
				}
			}
		});
		new Label(shlListBeast, SWT.NONE);
		new Label(shlListBeast, SWT.NONE);

		txtMaximumNumberOf = new Text(shlListBeast, SWT.NONE);
		txtMaximumNumberOf.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
		txtMaximumNumberOf.setEditable(false);
		txtMaximumNumberOf.setText("Max number of youtube results per artist");

		ComboViewer comboViewer = new ComboViewer(shlListBeast, SWT.NONE);
		maxYoutubeResults = comboViewer.getCombo();
		GridData gd_maxYoutubeResults = new GridData(SWT.LEFT, SWT.CENTER, false, false, 1, 1);
		gd_maxYoutubeResults.widthHint = 95;
		maxYoutubeResults.setLayoutData(gd_maxYoutubeResults);
		maxYoutubeResults.setItems(new String[] { "1", "2", "3", "4", "5", "6",
				"7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17",
				"18", "19", "20" });
		formToolkit.paintBordersFor(maxYoutubeResults);
		maxYoutubeResults.select(0);

		btnCopySelectedSongs = new Button(shlListBeast, SWT.CHECK | SWT.RIGHT);
		GridData gd_btnCopySelectedSongs = new GridData(SWT.LEFT, SWT.CENTER,
				false, false, 2, 1);
		gd_btnCopySelectedSongs.horizontalIndent = 4;
		btnCopySelectedSongs.setLayoutData(gd_btnCopySelectedSongs);
		btnCopySelectedSongs.setAlignment(SWT.LEFT);
		btnCopySelectedSongs
				.setText("Copy selected songs to destination folder");

		btnCreateMultiplePlaylists = new Button(shlListBeast, SWT.CHECK
				| SWT.RIGHT);
		GridData gd_btnCreateMultiplePlaylists = new GridData(SWT.LEFT,
				SWT.CENTER, false, false, 1, 1);
		gd_btnCreateMultiplePlaylists.horizontalIndent = 4;
		btnCreateMultiplePlaylists.setLayoutData(gd_btnCreateMultiplePlaylists);
		btnCreateMultiplePlaylists
				.setText("Create different playlist per artist");
		btnCreateMultiplePlaylists.setAlignment(SWT.LEFT);
		new Label(shlListBeast, SWT.NONE);

		Label label_10 = new Label(shlListBeast, SWT.SEPARATOR | SWT.HORIZONTAL);
		GridData gd_label_10 = new GridData(SWT.CENTER, SWT.FILL, false, false,
				1, 1);
		gd_label_10.widthHint = 281;
		label_10.setLayoutData(gd_label_10);
		new Label(shlListBeast, SWT.NONE);

		startButton = new Button(shlListBeast, SWT.NONE);
		GridData gd_btnNewButton = new GridData(SWT.CENTER, SWT.CENTER, false,
				false, 1, 1);
		gd_btnNewButton.widthHint = 364;
		startButton.setLayoutData(gd_btnNewButton);
		startButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				ListBeastApp myApp;
				try {
					myApp = new ListBeastApp(destDirText.getText(),
							sourceDirText.getText(), Integer
									.valueOf(maxYoutubeResults.getText()),
							btnCopySelectedSongs.getSelection(),
							btnCreateMultiplePlaylists.getSelection());

					myApp.registerObserver(ListBeastSWT.this);
					Thread popApp = new Thread(myApp);
					popApp.start();
				} catch (ApplicationException e1) {
					 // Create the required Status object
			        Status status = new Status(IStatus.ERROR, "My Plug-in ID", 0,
			        		e1.getMessage(), null);

			        // Display the dialog
			        ErrorDialog.openError(Display.getCurrent().getActiveShell(),
			            "Error", "An error has occured", status);
				}
			}
		});
		startButton.setText("Start");
		new Label(shlListBeast, SWT.NONE);

		progressBar = new ProgressBar(shlListBeast, SWT.SMOOTH);
		progressBar.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false,
				false, 1, 1));
		formToolkit.adapt(progressBar, true, true);
		new Label(shlListBeast, SWT.NONE);

		progressStatusText = new Text(shlListBeast, SWT.NONE);
		progressStatusText.setBackground(SWTResourceManager.getColor(SWT.COLOR_INFO_BACKGROUND));
		progressStatusText.setEditable(false);
		progressStatusText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER,
				true, false, 2, 1));

	}

	@Override
	public void update(final NotificationType notificationType,
			final NotificationData notificationData) {
		if (display.isDisposed())
			return;
		display.asyncExec(new Runnable() {
			public void run() {
				switch (notificationType) {
				case APP_STARTED:
					progressBar.setSelection(5);
					progressStatusText.setText(notificationData.getData());
					startButton.setEnabled(false);
					break;
				case UPDATE_PROGRESS_BAR:
					progressBar.setSelection(notificationData
							.getProgressPercentage());
					progressStatusText.setText(notificationData.getData());
					break;
				case APP_FINISHED:
					progressBar.setSelection(100);
					progressStatusText.setText(notificationData.getData());
					startButton.setEnabled(true);
					break;
				}
				if (progressBar.isDisposed())
					return;
			}
		});
	}
}
